<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="店铺名称" prop="cusname">
        <el-input
          v-model="queryParams.cusname"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="followList" border>
      <el-table-column label="序号" align="center" type="index" width="50" fixed></el-table-column>
      <el-table-column label="店铺名称" prop="cusname" :show-overflow-tooltip="true" header-align="center" />
      <el-table-column label="旺旺号" prop="aliww" :show-overflow-tooltip="true" header-align="center" />
      <el-table-column label="拥有者" prop="allocationUser" :show-overflow-tooltip="true" width="150" align="center" />
      <el-table-column label="客户状态" prop="status" :show-overflow-tooltip="true" width="150" align="center" />
      <el-table-column label="最近联系人" prop="contact" :show-overflow-tooltip="true" align="center" />
      <el-table-column label="最近联系电话" prop="tel" :show-overflow-tooltip="true" align="center" />
      <el-table-column label="最近联系时间" prop="createTime" :show-overflow-tooltip="true" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope"><el-button
          size="mini"
          type="text"
          icon="el-icon-circle-plus-outline"
          @click="handleAddFollow(scope.row)"
        >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-reading"
            @click="handleViewFollowRecords(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 跟进记录 -->
    <el-dialog :title="title" :visible.sync="openFollowDialog" width="700px" append-to-body>
      <div class="block">
        <el-timeline>
          <el-timeline-item v-for="(follow, index) in follows"
                            :key="index"
                            :timestamp="follow.createTime"
                            placement="top">
            <el-card>
              <p>
                <span style="font-weight: bold">跟进内容：</span><span>{{ follow.content }}</span>
              </p>
              <p>
                <span style="font-weight: bold">联系人：</span><span>{{ follow.contact }}</span>
              </p>
              <p>
                <span style="font-weight: bold">联系电话：</span><span>{{ follow.tel }}</span>
              </p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-dialog>

    <!-- 添加跟进记录 -->
    <el-dialog :title="title" :visible.sync="openAddFollowDialog" width="600px" append-to-body>
      <el-form ref="followForm" :model="followForm" :rules="followFormRules" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="跟进记录" prop="content">
              <el-input type="textarea" v-model="followForm.content"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="followForm.contact"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="followForm.tel"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFollowForm">确 定</el-button>
        <el-button @click="cancelFollowForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { insertFollow, selectFollow, selectFollowList } from '@/api/custom/custom'

export default {
  name: 'Follow',
  data(){
    return {
      queryParams: {
        cusname: '',
        pageNum: 1,
        pageSize: 10
      },
      loading: false,
      followList: [],
      total: 0,
      title: '',
      openFollowDialog: false,
      follows: [],
      followOptions: [],
      openAddFollowDialog: false,
      // 跟进记录表单
      followForm: {
        id: '',
        customId: '',
        contact: '',
        content: '',
        tel: '',
        intention: '',
        method: '',
        nextTime: undefined
      },
      // 添加跟进记录表单教校验
      followFormRules: {
        content: [
          { required: true, message: "沟通内容不能为空", trigger: ['blur', 'change'] },
          { min:1, max: 500, message: '长度应该为1-500个字符', trigger: ['blur', 'change'] }
        ],
        // contact: [
        //   { required: true, message: '联系人不能为空', trigger: ['blur', 'change'] },
        //   { min: 1, max: 30, message: '联系人长度应为1-30个字符', trigger: ['blur', 'change'] }
        // ],
        // method: [
        //   { required: true, message: '跟进方式不能为空', trigger: 'change' }
        // ]
      },
      // 跟进方式
      followMehtodOptions: []
    }
  },
  created() {
    this.getList()
    this.getDicts("follow_type").then(response => {
      this.followOptions = response.data;
    })
    this.getDicts("follow_type").then(response => {
      console.log(JSON.stringify(response.data))
      this.followMehtodOptions = response.data
    })
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 查询方法
    getList() {
      this.loading = true;
      selectFollowList(this.queryParams).then(response => {
        console.log(response.rows)
          this.followList = response.rows
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 查看跟进记录
    handleViewFollowRecords(row) {
      selectFollow(row.id).then(response => {
        this.follows = response.data
        this.title = '跟进记录'
        this.openFollowDialog = true
      })
    },
    // 跟进方式格式化
    followTypeFormat(type) {
      return this.selectDictLabel(this.followOptions, type);
    },
    // 打开添加跟进记录窗口
    handleAddFollow(row){
      this.followForm.customId= row.id
      this.title = '跟进记录'
      this.openAddFollowDialog = true
    },
    // 重置跟进记录表单
    resetFollowForm(){
      this.followForm.id = ''
      this.followForm.customId = ''
      this.followForm.content = ''
      this.followForm.contact = ''
      this.followForm.method = ''
      this.resetForm('followForm')
    },
    // 提交沟通记录表单
    submitFollowForm(){
      this.$refs['followForm'].validate(valid=>{
        if(valid){
          if(this.followForm.id === undefined || this.followForm.id === ''){
            let that = this
            this.$confirm('跟进记录保存后无法修改，是否继续保存？', '提示', {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function(){
              insertFollow(that.followForm).then(response => {
                console.log('=== ' + JSON.stringify(response))
                if(response.code === 200){
                  that.msgSuccess("新增成功");
                  that.openAddFollowDialog = false
                  that.resetFollowForm()
                  that.getList()
                }
              })
            })
          }
        }
      })
    },
    // 关闭沟通记录表单
    cancelFollowForm(){
      this.openAddFollowDialog = false
      this.resetFollowForm()
    }
  }
}
</script>



<style scoped lang="scss">

</style>
